#include "ShowPicsDialog.h"
#include "ui_ShowPicsDialog.h"

ShowPicsDialog::ShowPicsDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ShowPicsDialog)
{
    ui->setupUi(this);
    m_indexImage = 0;
}

ShowPicsDialog::~ShowPicsDialog()
{
    delete ui;
}

void ShowPicsDialog::paintEvent (QPaintEvent *)
{
    //1. 创建画家对象
    QPainter painter(this);
    //2. 获取绘制所在的区域
    QRect rcImage = ui->frame->frameRect();
    //qDebug() << "rcImage:" << rcImage;
    //qDebug() << "frame:" << ui->frame->pos();
    //坐标系的平移, rcImage和paint使用相同的坐标系
    rcImage.translate(ui->frame->pos());

    //3. 构建要绘制的图片, ":"表示资源路径
    QImage image (":/material/images/" + QString::number(m_indexImage) + ".jpg");
    //4. 使用画家对象, 把image绘制到rcimage中
    painter.drawImage(rcImage, image);
}

void ShowPicsDialog::on_m_btnPrev_clicked()
{
    if (--m_indexImage < 0) {
        m_indexImage = 9;
    }
    update ();//更新, 触发绘图事件
}

void ShowPicsDialog::on_m_btnNext_clicked()
{
    if (++m_indexImage > 9) {
        m_indexImage = 0;
    }
    update ();
}
